﻿using System;
using System.Collections.Generic;
using ProteinInsightInterface;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace ProteinInsightPlugin
{
    public class MyPlugin : MarshalByRefObject, IProteinInsightPlugin
    {
        public void Run(IProteinInsight pi)
        {
            try
            {
                //    write plug in code here.
                //    Example: pi.Open("1a31");             // Load Protein ID 1a31
                InputDialog inputDialog = new InputDialog();
                if ( inputDialog.ShowDialog() == DialogResult.Cancel )
                {
                    return;
                }

                double contactLen = inputDialog.contactLen;

                //  Find Processing two chains.
                int     numChain = 0;
                IChain  [] chain = new IChain[3];

                for ( int i = 0 ; i < pi.PDBs.Count ; i++ )
                {
                    for (int j = 0; j < pi.PDBs[i].Models.Count; j++)
                    {
                        for (int k = 0; k < pi.PDBs[i].Models[j].Chains.Count; k++)
                        {
                            if (pi.PDBs[i].Models[j].Chains[k].Select == true)
                            {
                                chain[numChain] = pi.PDBs[i].Models[j].Chains[k];
                                numChain++;
                                if (numChain >= 3)
                                    break;
                            }
                        }
                        if (numChain >= 3)
                            break;
                    }
                    if ( numChain >= 3 )
                        break;
                }

                if ( numChain != 2 )
                {
                    string msg = "Select two chains.";
                    pi.Utility.OutputMsg(msg);
                    MessageBox.Show(msg);
                    return;
                }

                //
                //  select contact surface.
                //
                pi.SetSelect(false, true);
                
                for (int i = 0; i < chain[0].Atoms.Count; i++)
                {
                    pi.Utility.SetProgressInStatusBar( (i + 1) * 100 / chain[0].Atoms.Count );
                    for (int j = 0; j < chain[1].Atoms.Count; j++)
                    {
                        Vector3 vec3Len = chain[0].Atoms[i].PositionTransformed - chain[1].Atoms[j].PositionTransformed;
                        if (vec3Len.Length() < contactLen)
                        {
                            chain[0].Atoms[i].SetSelect(true, false);
                        }
                    }
                }

                pi.UpdateSelect();

                pi.AddVP(IProteinInsight.IDisplayStyle.Surface);

                //
                //  select reverse side.
                //
                pi.SetSelect(false, true);

                for (int i = 0; i < chain[1].Atoms.Count; i++)
                {
                    pi.Utility.SetProgressInStatusBar((i + 1) * 100 / chain[1].Atoms.Count);
                    for (int j = 0; j < chain[0].Atoms.Count; j++)
                    {
                        Vector3 vec3Len = chain[1].Atoms[i].PositionTransformed - chain[0].Atoms[j].PositionTransformed;
                        if (vec3Len.Length() < contactLen)
                        {
                            chain[1].Atoms[i].SetSelect(true, false);
                        }
                    }
                }

                pi.UpdateSelect();

                pi.AddVP(IProteinInsight.IDisplayStyle.Surface);

            }
            catch (Exception e)
            {
                pi.Utility.OutputMsg(e.ToString());
            }
        }

        public String Name
        {
            get
            {
                return "Find Contact Surface";
            }
        }
    }
}

